﻿<?php

include_once "../json.php";
include_once "../conf/conf.inc"; 
include_once "../util/util.php";
include_once "../util/DBHelper.php";

function execFunc()
{
	$uid = GetRequest("uid");  
	$nickname = GetRequest("nickname");  
	$cmd = GetRequest("cmd"); 
	$timeline = GetRequest("timeline"); 
	$target_uid = GetRequest("target_uid");  #被操作的目标UID
	
	if($cmd == "sys_login" || $cmd == "up_photo")			//登陆 ,上传相片, 自己的操作， 写进关注我的人/暗恋我的人 的记录文本 
	{
		$sql = sprintf("select uid, nickname, attt_me from, hidden_love_me t_user_record where uid = %s", $uid);
		//WriteLog($sql);
		$rArray = DBHelper::Query($sql, $msg); 
		if($rArray != -1 && count($rArray) >0 )
		{
			$str_attt_me = $rArray[0]["attt_me"];
			$AtttMe = json_decode($str_attt_me, true);	//关注我的人
			foreach($AtttMe as $tmpUID => $userArray)
			{ 
				writeLogTxtExt($uid, $nickname, $cmd, $timeline, $tmpUID);
			}
			
			$str_hidden_love_me = $rArray[0]["hidden_love_me"];
			$HiddenLoveMe = json_decode($str_hidden_love_me, true);	//暗恋我的人
			foreach($HiddenLoveMe as $tmpUID => $userArray)
			{ 
				writeLogTxtExt($uid, $nickname, $cmd, $timeline, $tmpUID);
			}
		}
	}
	else  //对其他人的操作， 写进其他人的 记录文本 
	{	 
		writeLogTxtExt($uid, $nickname, $cmd, $timeline, $target_uid);
	}
}

//根据命令字 组织信息
function getInfoMsg($nickname, $cmd)
{
	$returnStr = "";
	
	if($cmd == "sys_login")			//登陆
	{
		$returnStr = "登陆了游戏";
	}
	else if($cmd == "up_photo")  	//上传相片
	{
		$returnStr = "上传了新的相片";
	}
	else if($cmd == "enter_house")  //访问好友房间
	{
		$returnStr = "访问过我的公寓";
	}
	else if($cmd == "buy_goods")  	//购买/赠送商品
	{
		$returnStr = "送了一件礼物给我";
	}
	
	return $returnStr;
}

/*  文件格式如下:

[
	{"uid":"1420","nickname":"xxx","cmd":"sys_login","timeline":"123456789", "msg":"上线了"},
	{"uid":"1420","nickname":"xxx","cmd":"sys_login","timeline":"123789789", "msg":"上线了"}
]

*/

function writeLogTxtExt($uid, $nickname, $cmd, $timeline, $target_uid)
{
		$tmpArray = array();
		$tmpArray["uid"] = $uid;
		$tmpArray["nickname"] = $nickname;
		$tmpArray["cmd"] = $cmd;
		$tmpArray["timeline"] = $timeline; 
		$tmpArray["msg"] = getInfoMsg($nickname, $cmd);
		 
		writeLogTxt($target_uid, $tmpArray);
}

//写文件(tmpUID:目标者ID, tmpMsgArray:消息内容数组)
function writeLogTxt($tmpUID, $tmpMsgArray)
{
	$newPath = getUploadPathExt($tmpUID);
	CreateFolder($newPath); //建立文件夹 
	$newfilename = "friend_info.txt";
	$fullPath = $newPath . $newfilename; //全文件名字
	
	$tmpArray = array();
	if(file_exists($fullPath))	//文件存在,则判断行数是否大于最多行数
	{ 
		$tmpStr = file_get_contents($fullPath); 	//读取文件
		$tmpArray = json_decode($tmpStr, true);		//字符串编码成数值
		
		//--判断当天是否已经写过同一个人同命令的记录，写过，则不再写
		if(isHadSameCmdMsgOneDay($tmpArray, $tmpMsgArray))
			return;
		
		$tmpLen = count($tmpArray);					//文件行数
		$fileLineMaxLen = GetConfInfo("friend_txt_line_count");			//文件最多行数
		if($tmpLen >= $fileLineMaxLen) 
			array_shift($tmpArray); 	//大于最多行数, 则删除首行
	}
	$tmpArray[] = $tmpMsgArray;
	$tmpMsg = json_encode($tmpArray); 	//编码成字符串 
	file_put_contents($fullPath, $tmpMsg); //写文件
	//print_r($tmpArray);
	//WriteLog($tmpMsg);
}

//当天是否已经写过同一个人同命令的记录
function isHadSameCmdMsgOneDay($tmpArray, $tmpMsgArray)
{
	if(count($tmpArray) < 1)
		return false;
	foreach($tmpArray as $tmpIndex => $tmpInfoArray)
	{
		if($tmpInfoArray["uid"] == $tmpMsgArray["uid"] 
		&& $tmpInfoArray["cmd"] == $tmpMsgArray["cmd"] 
		&& isSameDay($tmpInfoArray["timeline"], $tmpMsgArray["timeline"]))
			return true;
	}
	return false;
}

//是否同一天
function isSameDay($timeSecond_file, $timeSecond_new)
{
	$tmpDate1 = date("Y-m-d", $timeSecond_file);
	$tmpDate2 = date("Y-m-d", $timeSecond_new);
	return $tmpDate1 == $tmpDate2;
}

execFunc();
?>
